import java.util.Arrays;
import java.util.Scanner;

public class NGuessNumber {
    public static int[]guesslist=new int[4];
    public static int[] numbers=new int[4];
    public static int a_num = 0;
    public static int b_num = 0;
    private static Object Arrays;

    public static void main(String[] args) {

        System.out.println("|----新猜数字游戏-----|");

        while(true) {

            System.out.println("|----输入1开始游戏----|");
            System.out.println("|----输入0结束游戏----|\n");
            Scanner playerinput = new Scanner(System.in);

            switch (playerinput.nextInt()) {
                case 0:
                    System.out.println("----已退出游戏----\n");
                    System.exit(0);
                    break;
                case 1:
                    int cs = 0;
                    int sy = 7;

                    System.out.println("----已开始游戏(限制次数为7次)----\n");
                    generateAnswer();
                    while (true) {

                        getPlayerInput();
                        compareGuessAnswer();
                        //isWin();
                        if (iswin == true) {
                            System.out.println("玩家获胜\n");
                            break;
                        } else {
                            cs++;
                            sy--;
                            if(cs==7){
                                System.out.println("7次到了，正确答案为"+ Arrays.toString(numbers));
                                System.out.println("|----再来一局？------|");
                                break;
                            }
                            System.out.println("猜错了，在来一次！(还剩"+sy+"次)\n");
                            //System.out.println("答案："+Arrays.toString(numbers));
                        }
                    }
                    break;

                default:
                    System.out.println("错误的输入字符，请输入0/1\n");
            }
        }
    }


    public static int[] generateAnswer() {
        int a, b, c, d;
        int[] e = new int[]{};
        for (; ; ) {
            a = (int) (Math.random() * 9);
            b = (int) (Math.random() * 9);
            c = (int) (Math.random() * 9);
            d = (int) (Math.random() * 9);
            if (a != b && a != c && a != d && b != c && b != d) {
                e = new int[]{a, b, c, d};
                break;
            }
        }
        return e;
    }

    public static int[] getPlayerInput() {
        int f[] = new int[]{};
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入第一个数");
        int one = scanner.nextInt();
        System.out.println("请输入第二个数");
        int two = scanner.nextInt();
        System.out.println("请输入第三个数");
        int three = scanner.nextInt();
        System.out.println("请输入第四个数");
        int four = scanner.nextInt();
        f = new int[]{one, two, three, four};
        return f;


    }

    public static boolean iswin(){
        int e [] = generateAnswer();
        int f [] =getPlayerInput();
        int A = 0;
        for (int i = 0; i < e.length-1; i++) {
            for (int j = 0; j < f.length; j++) {
                if (e[i]==f[j]){
                    A++;
                }
            }
        }
        return Arrays;
    }
    public static String compareGuessAnswer(){
        a_num = 0;
        b_num = 0;

        for (int i = 0; i < numbers.length; i++) {
            for (int l=0; l < numbers.length; l++){
                if(i!=l){
                    if(numbers[i]==guesslist[l]){
                        b_num++;
                    }
                }else{
                    if(numbers[i]==guesslist[l]){
                        a_num++;
                    }
                }

            }
        }

        System.out.println(a_num+"A"+b_num+"B");
        System.out.println("A表示存在且位置正确的数字个数，B表示存在且位置不正确的数字个数！");
        System.out.println("如：1A2B,表示你猜测的四个数字有3个是对的，有一个位置对，两个位置不对。\n");

        return a_num+"A"+b_num+"B\n"+"A表示存在且位置正确的数字个数，B表示存在且位置不正确的数字个数！\n"+"如：1A2B,表示你猜测的四个数字有3个是对的，有一个位置对，两个位置不对。";
    }

}